home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / HTML / Progress / FTP / upload.php
Encoding:
PHP Script  |  2004-03-24  |  8.5 KB  |  268 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/3_0.txt.                                  |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Laurent Laville <pear@laurent-laville.org>                   |
  16. // | Credit: Tomas V.V.Cox <cox@vulcanonet.com>                           |
  17. // |         to reuse part of his code and idea from package HTTP_upload  |
  18. // |         see: http://pear.php.net/package/HTTP_Upload                 |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: upload.php,v 1.1 2004/02/14 22:02:14 farell Exp $
  22.  
  23. require_once 'PEAR.php';
  24.  
  25. /**
  26.  * The FTP_Upload class provides an easy and secure managment 
  27.  * of files to upload to your ftp server.
  28.  *
  29.  * @version    1.1
  30.  * @author     Laurent Laville <pear@laurent-laville.org>
  31.  * @author     Tomas V.V.Cox <cox@vulcanonet.com>
  32.  * @access     public
  33.  * @category   HTML
  34.  * @package    HTML_Progress
  35.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  36.  */
  37.  
  38. class FTP_Upload
  39. {
  40.     /**
  41.      * It's a common security risk in pages who has the upload dir
  42.      * facility. You should restrict file kind to upload.
  43.      *
  44.      * @var        array
  45.      * @since      1.1
  46.      * @access     private
  47.      * @see        setValidExtensions()
  48.      */
  49.     var $_extensions_check = array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'tar', 'zip', 'gz');
  50.  
  51.     /**
  52.      * A list of files ready to upload on a ftp server.
  53.      *
  54.      * @var        array
  55.      * @since      1.1
  56.      * @access     private
  57.      * @see        setFiles()
  58.      */
  59.     var $_files = array();
  60.  
  61.     /**
  62.      * FTP stream resource used for communications
  63.      *
  64.      * @var        resource
  65.      * @since      1.1
  66.      * @access     private
  67.      */
  68.     var $_conn;
  69.  
  70.  
  71.     /**
  72.      * The FTP upload class constructor
  73.      *
  74.      * @since      1.1
  75.      * @access     public
  76.      */
  77.     function FTP_Upload()
  78.     {
  79.         if (function_exists('version_compare') && version_compare(phpversion(), '4.3', 'ge')) {
  80.             // some ftp functions requires PHP 4.3+
  81.         } else {
  82.             trigger_error('PHP version 4.3 or better is required', E_USER_ERROR);
  83.     }
  84.     
  85.     if (!in_array('ftp', get_loaded_extensions())) {
  86.             trigger_error('extension ftp is unavailable', E_USER_ERROR);
  87.     }
  88.     }
  89.  
  90.     /**
  91.      * Restricts the valid extensions on file uploads.
  92.      *
  93.      * @param      mixed     $exts          File extensions to validate
  94.      *
  95.      * @return     void
  96.      * @since      1.1
  97.      * @access     public
  98.      */
  99.     function setValidExtensions($exts)
  100.     {
  101.         if (is_array($exts)) {
  102.             $this->_extensions_check = $exts;
  103.         } else {
  104.             $this->_extensions_check = array();
  105.             array_push($this->_extensions_check, $exts);
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Set a list of files to upload on the ftp server.
  111.      *
  112.      * @param      mixed     $files         List of files to transfer to FTP server.
  113.      * @param      boolean   $check         (optional) Restrict files to valid extensions only.
  114.      *
  115.      * @return     void
  116.      * @since      1.1
  117.      * @access     public
  118.      */
  119.     function setFiles($files, $check = true)
  120.     {
  121.         $this->_files = $inputs = array();
  122.         if (is_array($files)) {
  123.             $inputs = $files;
  124.         } else {
  125.             array_push($inputs, $files);
  126.         }
  127.  
  128.         foreach ($inputs as $file) {
  129.             if ($check) {
  130.                 $info = pathinfo($file);
  131.                 if (in_array($info['extension'], $this->_extensions_check) && file_exists($file)) {
  132.                     $this->_files[] = $file;
  133.                 }
  134.             } else {
  135.                 $this->_files[] = $file;
  136.         }
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Connect on a remote FTP server and login as $user.
  142.      *
  143.      * @param      string    $host          FTP server to connect to.
  144.      * @param      string    $user          Username.
  145.      * @param      string    $pass          Password.
  146.      *
  147.      * @return     mixed                    TRUE on success, and PEAR_Error on failure
  148.      * @since      1.1
  149.      * @access     public
  150.      */
  151.     function logon($user, $pass, $host, $port = 21, $timeout = 90)
  152.     {
  153.         $conn = $this->_connect($host, $port, $timeout);
  154.         if (PEAR::isError($conn)) {
  155.             return $conn;
  156.         }
  157.         $this->_conn = $conn;
  158.  
  159.         $logs = $this->_login($conn, $user, $pass);
  160.         if (PEAR::isError($logs)) {
  161.             $this->logoff();            
  162.             return $logs;
  163.         }
  164.     }
  165.  
  166.     /**
  167.      * Disconnect from a remote FTP server.
  168.      * (Timeout is default set to 90 sec.)
  169.      *
  170.      * @return     void
  171.      * @since      1.1
  172.      * @access     public
  173.      */
  174.     function logoff()
  175.     {
  176.         @ftp_close($this->_conn);
  177.         unset($this->_conn);
  178.     }
  179.  
  180.     /**
  181.      * Uploads the files synchronously into directory $dest on remote host.
  182.      *
  183.      * @param      string    $dest          Changes from current to the specified directory.
  184.      *
  185.      * @return     mixed                    PEAR_Error on failure, array (null) of untransfered files
  186.      * @since      1.1
  187.      * @access     public
  188.      */
  189.     function moveTo($dest)
  190.     {
  191.         $dir = $this->_changeDir($dest);
  192.         if (PEAR::isError($dir)) {
  193.             return $dir;
  194.         }
  195.         $nomove = array();   // files not transfered on remote host
  196.  
  197.         foreach ($this->_files as $file) {
  198.             
  199.             $ret = ftp_put($this->_conn, basename($file), $file, FTP_BINARY);
  200.             if (!$ret) {
  201.                 $nomove[] = $file;
  202.             }
  203.         }
  204.         return $nomove;
  205.     }
  206.  
  207.     /**
  208.      * Changes directories on a FTP server.
  209.      *
  210.      * @param      string    $dest          Changes from current to the specified directory.
  211.      *
  212.      * @return     mixed                    TRUE on success, and PEAR_Error on failure
  213.      * @since      1.1
  214.      * @access     private
  215.      */
  216.     function _changeDir($dest)
  217.     {
  218.         if (!isset($this->_conn)) {
  219.             return PEAR::raiseError('You should logs in fisrt'); 
  220.         }
  221.         $chg = ftp_chdir($this->_conn, $dest);
  222.         if (!$chg) {
  223.             return PEAR::raiseError('Couldn\'t change to directory ' . $dest); 
  224.         }
  225.         return true;
  226.     }
  227.  
  228.     /**
  229.      * Opens an FTP connection on remote host.
  230.      *
  231.      * @param      string    $host          Hostname.
  232.      * @param      integer   $port          (optional) an alternate port to connect to.
  233.      * @param      integer   $timeout       (optional) the timeout for all subsequent network operations.
  234.      *
  235.      * @return     mixed                    FTP stream on success, and PEAR_Error on failure
  236.      * @since      1.1
  237.      * @access     private
  238.      */
  239.     function _connect($host, $port = 21, $timeout = 90)
  240.     {
  241.         $conn = ftp_connect($host, $port, $timeout);
  242.         if ($conn === false) {
  243.             return PEAR::raiseError('Couldn\'t connect to ' . $host); 
  244.         }
  245.         return $conn;
  246.     }
  247.  
  248.     /**
  249.      * Logs in to an FTP connection.
  250.      *
  251.      * @param      resource  $conn          FTP stream resource.
  252.      * @param      string    $user          Username.
  253.      * @param      string    $pass          Password.
  254.      *
  255.      * @return     mixed                    TRUE on success, and PEAR_Error on failure
  256.      * @since      1.1
  257.      * @access     private
  258.      */
  259.     function _login($conn, $user, $pass)
  260.     {
  261.         $logs = ftp_login($conn, $user, $pass);
  262.         if (!$logs) {
  263.             return PEAR::raiseError('Couldn\'t connect as ' . $user); 
  264.         }
  265.         return true;
  266.     }
  267. }
  268. ?>